const host =  "https://yulemen.fun/api/"
let token = '';
function reqeust(url, method, params,tipsType) {
  tipsType = tipsType || 1;

  if (!token) {
    token = wx.getStorageSync('token');
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: host + url, // 目标服务器url
      data: params,
      header: {
        'Authorization': token ? `${token}` : '',
      },
      method: method,
      success: (res) => {
        if (res.data.code === 200) {
          resolve(res.data.data)
        } else {
          let title;
          console.log(tipsType, "提示类型")
          if(tipsType == 2){
            title = `${res.data.message}`;
          }else {
            title = `接口异常，原因如下：${res.data.message}`;
          };
          wx.showToast({
            title: title,
            icon: 'none'
          });
          reject(res)
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络错误...',
          icon: 'loading'
        });
        reject(res)
      }
    });
  })
}

//POST请求
function post(url, params,tipsType) {
  return reqeust(url, "POST", params,tipsType)
}

//GET请求
function get(url, params,tipsType) {
  return reqeust(url, "GET", params,tipsType)
}

export default {
  post,
  get
}